﻿$(document).ready(function () {
    //datepicker
    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
    $('.datepicker').datepicker();
    $('.datetimepicker').datetimepicker();
    //tabs
    var tabID = getUrlParam("tab");
    if (tabID != null) {
        $('#myTab #' + tabID).tab('show');
    } else {
        $('#myTab a:first').tab('show');
    }
    $('#myTab a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    });
    //查询条件复选
    $("#ajax_spin").change(function () {
        SpInChanged($("#ajax_spin").val());
    });
    $("#ajax_rdin").change(function () {
        RdInChanged($("#ajax_rdin").val());
    });
    if ($("#ajax_scin").length > 0) {
        $("#ajax_scin").change(function () {
            ScInChanged($("#ajax_scin").val());
        });
    }
    $(".mulselect").multiselect({
        selectedText: function (numChecked, numTotal, checkedInputs) {
            // example: emulating the selectedList option
            return (numChecked <= 5) ? checkedInputs.map(function (element) { return element.title; }).join(',') : numChecked + " 项已选择";
        }
    });
    $(".mulselect").multiselect({
        close: function (event, ui) {
            // event handler here
            var vals = "";
            $(this).find("option:selected").each(function () { vals += $(this).text() + ","; });
            $("#mulpanel_" + $(this).attr("id")).text(vals);
        }
    });
    $(".mulselect").each(function () {
        var vals = "";
        $(this).find("option:selected").each(function () { vals += $(this).text() + ","; });
        $("#mulsel_callback").append("<div id='mulpanel_" + $(this).attr("id") + "'>" + vals + "</div><br/>");
    });
    $("#mul_sel_ck").click(function () {
        $("#mulsel_callback").toggleClass("mul_show", 100, "easeOutSine");
    });
});

function SpInChanged(spin) {
    var params = "&st=y&spin=" + spin+"&dev="+$("#h_dev").val();
    var ajaxurl = "/include/api.aspx";
    if (spin == null) {return;}
    $.ajax({
        type: 'POST', url: ajaxurl, data: "action=getmulsclist" + params + "&nocache=" + Math.random(), dataType: 'json',
        success: function (ret) {
            if (ret.responseStatus == 200 && $("#ajax_scin").length > 0)//right
            {
                $("#ajax_scin").empty();
                var data = eval(ret.data); 
                for (var i = 0; i < data.length; i++) {
                    $("#ajax_scin").append("<option value='" + data[i].id + "'>" + data[i].text + "</option>");
                }
                $("#ajax_scin").multiselect('refresh');
                
            } else {
                if (ret.responseStatus == 500) {
                    //alert('无法处理的异常，请检查网络是否正常');
                }
            }
        }
    });
    $.ajax({
        type: 'POST', url: ajaxurl, data: "action=getmulcnlistBySp" + params + "&nocache=" + Math.random(), dataType: 'json',
        success: function (ret) {
            if (ret.responseStatus == 200 &&  $("#ajax_cnin").length>0)//right
            {
                $("#ajax_cnin").empty();
                var data = eval(ret.data);
                if ($("#ajax_cnin").length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        $("#ajax_cnin").append("<option value='" + data[i].text + "'>" + data[i].text + "</option>");
                    }
                    $("#ajax_cnin").multiselect('refresh');
                }
            } else {
                if (ret.responseStatus == 500) {
                    //alert('无法处理的异常，请检查网络是否正常');
                }
            }
        }
    });
}
function ScInChanged(scin) {
    var params = "&st=y&scin=" + scin + "&dev=" + $("#h_dev").val();
    var ajaxurl = "/include/api.aspx";
    if (scin == null) {return;}
    $.ajax({
        type: 'POST', url: ajaxurl, data: "action=getmulcnlist" + params + "&nocache=" + Math.random(), dataType: 'json',
        success: function (ret) {
            if (ret.responseStatus == 200)//right
            {
                $("#ajax_cnin").empty();
                var data = eval(ret.data);
                if ($("#ajax_cnin").length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        $("#ajax_cnin").append("<option value='" + data[i].text + "'>" + data[i].text + "</option>");
                    }
                    $("#ajax_cnin").multiselect('refresh');
                }
            } else {
                if (ret.responseStatus == 500) {
                    //alert('无法处理的异常，请检查网络是否正常');
                }
            }
        }
    });
}
function RdInChanged(rdin) {
    var params = "&st=y&rdin=" + rdin;
    var ajaxurl = "/include/api.aspx";
    if (rdin == null) {return;}
    $.ajax({
        type: 'POST', url: ajaxurl, data: "action=getmulplazalist" + params + "&nocache=" + Math.random(), dataType: 'json',
        success: function (ret) {
            $("#ajax_plazain").empty();
            if (ret.responseStatus == 200)//right
            {
                var data = eval(ret.data);
                for (var i = 0; i < data.length; i++) {
                    $("#ajax_plazain").append("<option value='" + data[i].id + "'>" + data[i].text + "</option>");
                }
                $("#ajax_plazain").multiselect('refresh');
            } else {
                if (ret.responseStatus == 500) {
                    //alert('无法处理的异常，请检查网络是否正常');
                }
            }
        }
    });
}
function getUrlParam(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r!=null) return unescape(r[2]); return null; //返回参数值
}